//
//  AppDelegate.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/15.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
//        //加载用户数据
//       let userAccount = ELUserAccount.loadUserAccount()
//        print("uesrAccount = \(userAccount)")
        
        //如果有用户数据则判断是否是新版本
//        if userAccount != nil && isNewVersion() 
        
        //是就显示新特征版面
        
        //否就欢迎归来
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor . whiteColor()
        
        window?.rootViewController = defaultViewController()
        
        window?.makeKeyAndVisible()
        
        return true
        
    }
    
    ///根据情况设置显示控制器
    private func defaultViewController() -> UIViewController {
        
        if !ELUserAccount.hasUserInfo {
            return ELMainTabBarController()
        }
        
        return isNewVersion() ? ELGuideViewController() : ELNewFeatureController()
    }
    
    ///判断是否为新版本
    private func isNewVersion() -> Bool {
        //得到当前版本号
        let newVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        //和上一次比较是否为新版本
        let keyString = "sandBoxVersionKey"
        let oldVersion = NSUserDefaults.standardUserDefaults().stringForKey(keyString)
        
        let result : Bool = newVersion != oldVersion
        if  result {
            NSUserDefaults.standardUserDefaults().setObject(newVersion, forKey: keyString)
        }
        
        return result
    }
    
//    ///动画切换控制器
//    func animationChangeViewController (targetController : UIViewController , currentView : UIView) {
//        
//        window?.rootViewController = targetController
//        
//        window?.addSubview(currentView)
//        
//        UIView.animateWithDuration(0.8, animations: { () -> Void in
//            
//            currentView.frame.origin.y -= (self.window?.bounds.height)!
//            
//            }) { (_) -> Void in
//                currentView.removeFromSuperview()
//        }
//    }
}

